package com.vf.admin.client.service.impl;

import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dao.*;
import com.vf.admin.client.service.*;
import com.vf.admin.client.tx.utils.TxIm;
import com.vf.admin.client.tx.vo.testVo;
import com.vf.admin.common.Constants;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.fiwork.controller.AppDailyTasController;
import com.vf.admin.miaodong.entity.*;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.hutool.core.util.StrUtil.uuid;

@Service
public class AppGiftServiceImpl implements AppGiftService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppSetupService appSetupService;
    @Autowired
    private AppMingxiRoomService appMingxiRoomService;
    @Autowired
    private AppGiftDao appGiftDao;
    @Autowired
    private AppUserDao appUserDao;
    @Autowired
    private AppUserGiftDao appUserGiftDao;
    @Autowired
    private AppRoomMeiliDao appRoomMeiliDao;
    @Autowired
    private AppMingxiMeilizhiDao appMingxiMeilizhiDao;
    @Autowired
    private AppKnapsackDao appKnapsackDao;
    @Autowired
    private AppMingXiShouLiDao appMingXiShouLiDao;
    @Autowired
    private AppMingXiSongLiDao appMingXiSongLiDao;
    @Autowired
    private AppMingxiJinbiDao appMingxiJinbiDao;
    @Autowired
    private AppMingXiZuanShiDao appMingXiZuanShiDao;
    @Autowired
    private AppFamilyMemberService appFamilyMemberService;
    @Autowired
    private AppFamilyPrestigeService appFamilyPrestigeService;
    @Autowired
    private AppBeckoningService appBeckoningService;
    @Autowired
    private AppIntimacyGuardService appIntimacyGuardService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    private AppDailyTasController appDailyTasController;
    @Autowired
    private AppInvitationRecordService appInvitationRecordService;
    @Autowired
    private AppMingxiJinbiService appMingxiJinbiService;
    @Autowired
    private MessageChatFromService messageChatFromService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AppIntimacyGuardDao appIntimacyGuardDao;
    @Autowired
    private SystemAlertService systemAlertService;

    @Autowired
    private ExpressionsCommonService expressionsCommonService;

    @Override
    public List<AppGift> findList(Map<String, Object> paraMap, SplitPageBean splitPage) {
        return appGiftDao.findList(paraMap, splitPage);
    }

    @Override
    public AppGift get(Integer id) {
        return appGiftDao.get(id);
    }

    @Override
    public void save(AppGift haoYou) {
        appGiftDao.save(haoYou);
    }

    @Override
    public void del(Integer id) {
        appGiftDao.del(id);
    }

    @Override
    public List<AppGift> findByList(Map<String, Object> params, SplitPageBean splitPageBean) {
        return appGiftDao.findByList(params, splitPageBean);
    }

    @Override
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean roomGift(String[] str, AppUser user, BigDecimal giftmoney, AppGift gift, AppGiftNum giftNum, AppRoom room) {
        try {
            AppSetup setup = appSetupService.get();
            for (String s : str) {

                AppUser sluser = appUserDao.getTengxuncodeByUser(Integer.valueOf(s));

                BigDecimal getMoney = new BigDecimal("0");
                BigDecimal getCharm = new BigDecimal("0");
                if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                    AppSetup appSetup = appSetupService.get();
                    getMoney = giftmoney.multiply(appSetup.getJinDouExchangeForDiamonds()).setScale(2, BigDecimal.ROUND_HALF_UP);
                    getCharm = giftmoney.multiply(new BigDecimal((appSetup.getDiamondsToCharmGrowth())).setScale(0, BigDecimal.ROUND_DOWN));
                }
                //增加收礼人金币
                appUserDao.updatejinbi(sluser.getId(), getMoney);
                //增加收礼人魅力值
                appUserService.addCharm(sluser.getId(), (int) getCharm.longValue(), "礼物赠送");
                AppMingxiJinbi zuanShi1 = new AppMingxiJinbi();
                zuanShi1.setUserid(sluser.getId());
                zuanShi1.setPic(sluser.getPic());
                zuanShi1.setPhone(sluser.getLoginname());
                zuanShi1.setNick(sluser.getNick());
                zuanShi1.setLeixing(1);
                zuanShi1.setNum(getMoney);
                zuanShi1.setCreatetime(new Date());
                zuanShi1.setBeizhu("礼物收入");
                appMingxiJinbiDao.save(zuanShi1);

                int moneyleixing = 0;
                if (gift.getGoumaifangshi() == Constants.goumaifangshi_jinbi) {
                    moneyleixing = 2;
                    //金币礼物
                    //减少送礼人金币
                    appUserDao.upJianjinbi(user.getId(), giftmoney);
                    //增加送礼人金币明细
                    AppMingxiJinbi zuanShi = new AppMingxiJinbi();
                    zuanShi.setUserid(user.getId());
                    zuanShi.setPic(user.getPic());
                    zuanShi.setPhone(user.getLoginname());
                    zuanShi.setNick(user.getNick());
                    zuanShi.setLeixing(2);
                    zuanShi.setNum(giftmoney);
                    zuanShi.setCreatetime(new Date());
                    zuanShi.setBeizhu("礼物赠送");
                    appMingxiJinbiDao.save(zuanShi);
                } else if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                    moneyleixing = 1;
                    //钻石礼物
                    //减少送礼人钻石
                    appUserDao.upJianZuanShi(user.getId(), new BigDecimal(String.valueOf(giftmoney)));
                    //增加送礼人钻石明细
                    AppMingXiZuanShi zuanShi = new AppMingXiZuanShi();
                    zuanShi.setUserid(user.getId());
                    zuanShi.setPic(user.getPic());
                    zuanShi.setPhone(user.getLoginname());
                    zuanShi.setNick(user.getNick());
                    zuanShi.setLeixing(2);
                    zuanShi.setNum(giftmoney);
                    zuanShi.setCreatetime(new Date());
                    zuanShi.setBeizhu("礼物赠送");
                    appMingXiZuanShiDao.saveOrUpdate(zuanShi);
                }

                //记录收礼明细
                AppMingXiShouLi shouLi = new AppMingXiShouLi();
                shouLi.setCreatetime(new Date());
                shouLi.setNick(sluser.getNick());
                shouLi.setPic(sluser.getPic());
                shouLi.setUserid(sluser.getId());
                shouLi.setPhone(sluser.getLoginname());
                shouLi.setGiftid(gift.getId());
                shouLi.setGiftname(gift.getGiftname());
                shouLi.setGiftpic(gift.getPicture());
                shouLi.setLiwunum(giftNum.getNum());
                shouLi.setFujianum(giftmoney);
                shouLi.setSluserid(user.getId());
                shouLi.setSlusernick(user.getNick());
                shouLi.setSluserphone(user.getLoginname());
                shouLi.setSluserpic(user.getPic());
                shouLi.setRoomid(room.getId());
                shouLi.setRoomcode(room.getRoomcode());
                appMingXiShouLiDao.saveOrUpdate(shouLi);

                //记录送礼明细
                AppMingXiSongLi songLi = new AppMingXiSongLi();
                songLi.setCreatetime(new Date());
                songLi.setNick(user.getNick());
                songLi.setPic(user.getPic());
                songLi.setUserid(user.getId());
                songLi.setPhone(user.getLoginname());
                songLi.setGiftid(gift.getId());
                songLi.setGiftname(gift.getGiftname());
                songLi.setGiftpic(gift.getPicture());
                songLi.setLiwunum(giftNum.getNum());
                songLi.setFujianum(giftmoney.longValue());
                songLi.setSluserid(sluser.getId());
                songLi.setSlusernick(sluser.getNick());
                songLi.setSluserphone(sluser.getLoginname());
                songLi.setSluserpic(sluser.getPic());
                appMingXiSongLiDao.saveOrUpdate(songLi);
                //添加用户礼物墙
                AppUserGift appUserGift = appUserGiftDao.getGift(sluser.getId(), gift.getId());
                if (org.springframework.util.StringUtils.isEmpty(appUserGift)) {
                    AppUserGift userGift = new AppUserGift();
                    userGift.setUserid(sluser.getId());
                    userGift.setCreatetime(new Date());
                    userGift.setGiftid(gift.getId());
                    userGift.setGiftname(gift.getGiftname());
                    userGift.setGiftnum(giftNum.getNum());
                    userGift.setPicture(gift.getPicture());
                    userGift.setPhone(sluser.getLoginname());
                    userGift.setPic(sluser.getPic());
                    userGift.setNick(sluser.getNick());
                    appUserGiftDao.saveOrUpdate(userGift);
                } else {
                    appUserGiftDao.updateGiftNum(sluser.getId(), gift.getId(), giftNum.getNum());
                }

                //增加房间魅力
                AppRoomMeili roomMeili = appRoomMeiliDao.getMeili(sluser.getId(), room.getId());
                long meilizhis = 0;
                if (org.springframework.util.StringUtils.isEmpty(roomMeili)) {
                    //没有记录,保存新纪录
                    AppRoomMeili meili = new AppRoomMeili();
                    meili.setCreatetime(new Date());
                    meili.setUserid(sluser.getId());
                    meili.setUsercode(sluser.getUsercode());
                    meili.setMeilizhi(giftmoney.longValue());
                    meili.setPhone(sluser.getLoginname());
                    meili.setPic(sluser.getPic());
                    meili.setRoomcode(room.getRoomcode());
                    meili.setRoomid(room.getId());
                    appRoomMeiliDao.save(meili);
                    meilizhis = giftmoney.longValue();
                } else {
                    meilizhis = roomMeili.getMeilizhi() + giftmoney.longValue();
                    //有记录，增加房间魅力值
                    appRoomMeiliDao.addMeili(sluser.getId(), room.getId(), giftmoney.longValue());
                }
                appUserDao.updatemeili(sluser.getId(), giftmoney.longValue());
                //添加房间流水
                appMingxiRoomService.addMIngxi(user, giftmoney, moneyleixing, 2, "赠送礼物", room);

                String uuid = uuid();
                testVo tv = new testVo();
                tv.setGiftid(gift.getId());
                tv.setGiftmoney(giftmoney.longValue());
                tv.setGiftNumid(giftNum.getId());
                tv.setRoomid(room.getId());
                tv.setUserid(user.getId());
                tv.setSluserid(Integer.valueOf(s));
                tv.setMeilizhis(meilizhis);
                net.sf.json.JSONObject obj2 = net.sf.json.JSONObject.fromObject(tv);
                String json2 = obj2.toString();
                ClientConstants.xiaoxiCacheMap.put(uuid, json2);
                //增加邀请用户礼物收益提成
                addAward(sluser.getId(), getMoney);
                //添加送礼物任务
                appDailyTasController.getAwardOnce(user.getId(), ClientConstants.TaskOnce.GIFT_ONCE);

                String key = String.format(RedisKeys.USER, sluser.getId());
                redisUtil.del(key);
                key = String.format(RedisKeys.USER, user.getId());
                redisUtil.del(key);

                /**************收、送礼物增加家族威望值开始***************/
                AppFamilyMember slFamilyMember = appFamilyMemberService.findByUserId(sluser.getId());
                if (slFamilyMember != null && slFamilyMember.getStatus() == 1) {
                    if (setup.getShouliFamilyPrestige() > 0) {
                        appFamilyPrestigeService.addPrestisge(1, setup.getShouliFamilyPrestige(), sluser.getId(), "获赠礼物", slFamilyMember.getFid());
                    }
                }
                AppFamilyMember songFamilyMember = appFamilyMemberService.findByUserId(user.getId());
                if (songFamilyMember != null && songFamilyMember.getStatus() == 1) {
                    if (setup.getSongliFamilyPrestige() > 0) {
                        appFamilyPrestigeService.addPrestisge(2, setup.getSongliFamilyPrestige(), user.getId(), "赠送礼物", songFamilyMember.getFid());
                    }
                }
                /**************收、送礼物增加家族威望值结束***************/

                /*************************增加亲密度开始*******************/
                //双方亲密度，改成单方亲密度
                AppIntimacyGuard appIntimacyGuard = new AppIntimacyGuard();
                BigDecimal intimacy = giftmoney.multiply(setup.getIntimacyZuanshi());
                appIntimacyGuard.setUserId(user.getId());
                appIntimacyGuard.setBeiUserId(sluser.getId());
                appIntimacyGuard.setIntimacy(intimacy);
                if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                    appIntimacyGuard.setGiftMoney(giftmoney);
                } else {
                    appIntimacyGuard.setGiftMoney(getMoney);
                }
                appIntimacyGuard.setCreatetime(new Date());
                addIntimacy(appIntimacyGuard);
                appIntimacyGuard.setUserId(sluser.getId());
                appIntimacyGuard.setBeiUserId(user.getId());
                appIntimacyGuard.setId(null);
                addIntimacy(appIntimacyGuard);
                /*************************增加亲密度结束*******************/
            }
            return true;
        } catch (Exception e) {
            logger.error("异常:", e);
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Transactional
    public AppIntimacyGuard addIntimacy(AppIntimacyGuard appIntimacyGuard){
        AppIntimacyGuard appIntimacyGuard1 = appIntimacyGuardDao.addIntimacyGuard(appIntimacyGuard);
        appIntimacyGuard.setId(appIntimacyGuard1.getId());
        return appIntimacyGuardService.addOrSaveIntimacyGuard(appIntimacyGuard);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean beibaoRoomGift(String[] str, AppUser user, AppKnapsack knapsack, AppGift gift, AppGiftNum giftNum, AppRoom room) {
        try {
            for (String s : str) {
                long giftmoney = (giftNum.getNum() * gift.getGoldmoney());
                AppUser sluser = appUserDao.getTengxuncodeByUser(Integer.valueOf(s));
                //增加收礼人魅力值明细
                AppMingxiMeilizhi meilizhi = new AppMingxiMeilizhi();
                meilizhi.setUserid(sluser.getId());
                meilizhi.setPic(sluser.getPic());
                meilizhi.setPhone(sluser.getLoginname());
                meilizhi.setNick(sluser.getNick());
                meilizhi.setLeixing(1);
                meilizhi.setNum(new Long(giftmoney).intValue());
                meilizhi.setCreatetime(new Date());
                meilizhi.setBeizhu("赠送礼物");
                appMingxiMeilizhiDao.save(meilizhi);
                //减少背包礼物数量
                appKnapsackDao.jianshao(knapsack.getId(), giftNum.getNum());
                //记录收礼明细
                AppMingXiShouLi shouLi = new AppMingXiShouLi();
                shouLi.setCreatetime(new Date());
                shouLi.setNick(sluser.getNick());
                shouLi.setPic(sluser.getPic());
                shouLi.setUserid(sluser.getId());
                shouLi.setPhone(sluser.getLoginname());
                shouLi.setGiftid(gift.getId());
                shouLi.setGiftname(gift.getGiftname());
                shouLi.setGiftpic(gift.getPicture());
                shouLi.setLiwunum(giftNum.getNum());
                shouLi.setFujianum(new BigDecimal(giftmoney));
                shouLi.setSluserid(user.getId());
                shouLi.setSlusernick(user.getNick());
                shouLi.setSluserphone(user.getLoginname());
                shouLi.setSluserpic(user.getPic());
                shouLi.setRoomid(room.getId());
                shouLi.setRoomcode(room.getRoomcode());
                appMingXiShouLiDao.saveOrUpdate(shouLi);

                //记录送礼明细
                AppMingXiSongLi songLi = new AppMingXiSongLi();
                songLi.setCreatetime(new Date());
                songLi.setNick(user.getNick());
                songLi.setPic(user.getPic());
                songLi.setUserid(user.getId());
                songLi.setPhone(user.getLoginname());
                songLi.setGiftid(gift.getId());
                songLi.setGiftname(gift.getGiftname());
                songLi.setGiftpic(gift.getPicture());
                songLi.setLiwunum(giftNum.getNum());
                songLi.setFujianum(giftmoney);
                songLi.setSluserid(sluser.getId());
                songLi.setSlusernick(sluser.getNick());
                songLi.setSluserphone(sluser.getLoginname());
                songLi.setSluserpic(sluser.getPic());
                appMingXiSongLiDao.saveOrUpdate(songLi);
                //添加用户礼物墙
                AppUserGift appUserGift = appUserGiftDao.getGift(sluser.getId(), gift.getId());
                if (org.springframework.util.StringUtils.isEmpty(appUserGift)) {
                    AppUserGift userGift = new AppUserGift();
                    userGift.setUserid(sluser.getId());
                    userGift.setCreatetime(new Date());
                    userGift.setGiftid(gift.getId());
                    userGift.setGiftname(gift.getGiftname());
                    userGift.setGiftnum(giftNum.getNum());
                    userGift.setPicture(gift.getPicture());
                    userGift.setPhone(sluser.getLoginname());
                    userGift.setPic(sluser.getPic());
                    userGift.setNick(sluser.getNick());
                    appUserGiftDao.saveOrUpdate(userGift);
                } else {
                    appUserGiftDao.updateGiftNum(sluser.getId(), gift.getId(), giftNum.getNum());
                }
                //增加房间魅力
                AppRoomMeili roomMeili = appRoomMeiliDao.getMeili(sluser.getId(), room.getId());
                long meilizhis = 0;
                if (org.springframework.util.StringUtils.isEmpty(roomMeili)) {
                    //没有记录,保存新纪录
                    AppRoomMeili meili = new AppRoomMeili();
                    meili.setCreatetime(new Date());
                    meili.setUserid(sluser.getId());
                    meili.setUsercode(sluser.getUsercode());
                    meili.setMeilizhi(giftmoney);
                    meili.setPhone(sluser.getLoginname());
                    meili.setPic(sluser.getPic());
                    meili.setRoomcode(room.getRoomcode());
                    meili.setRoomid(room.getId());
                    appRoomMeiliDao.save(meili);
                    meilizhis = giftmoney;
                } else {
                    meilizhis = roomMeili.getMeilizhi() + giftmoney;
                    //有记录，增加房间魅力值
                    appRoomMeiliDao.addMeili(sluser.getId(), room.getId(), giftmoney);
                }

                appUserDao.updatemeili(sluser.getId(), giftmoney);
                String uuid = uuid();
                testVo tv = new testVo();
                tv.setGiftid(gift.getId());
                tv.setGiftmoney(giftmoney);
                tv.setGiftNumid(giftNum.getId());
                tv.setRoomid(room.getId());
                tv.setUserid(user.getId());
                tv.setSluserid(Integer.valueOf(s));
                tv.setMeilizhis(meilizhis);
                net.sf.json.JSONObject obj2 = net.sf.json.JSONObject.fromObject(tv);
                String json2 = obj2.toString();
                ClientConstants.xiaoxiCacheMap.put(uuid, json2);
            }
            return true;
        } catch (Exception e) {
            logger.error("赠送礼物异常:", e);
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean userGift(AppUser sluser, AppUser user, BigDecimal giftmoney, AppGift gift, AppGiftNum giftNum) {
        try {
            BigDecimal getMoney = new BigDecimal("0");
            BigDecimal getCharm = new BigDecimal("0");
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                AppSetup appSetup = appSetupService.get();
                getMoney = giftmoney.multiply(appSetup.getJinDouExchangeForDiamonds()).setScale(2, BigDecimal.ROUND_HALF_UP);
                getCharm = giftmoney.multiply(new BigDecimal((appSetup.getDiamondsToCharmGrowth())).setScale(0, BigDecimal.ROUND_DOWN));
            }
            //增加收礼人金币
            appUserDao.updatejinbi(sluser.getId(), getMoney);
            //增加收礼人魅力值
            appUserService.addCharm(sluser.getId(), (int) getCharm.longValue(), "获赠礼物");

            AppMingxiJinbi zuanShi1 = new AppMingxiJinbi();
            zuanShi1.setUserid(sluser.getId());
            zuanShi1.setPic(sluser.getPic());
            zuanShi1.setPhone(sluser.getLoginname());
            zuanShi1.setNick(sluser.getNick());
            zuanShi1.setLeixing(1);
            zuanShi1.setNum(getMoney);
            zuanShi1.setCreatetime(new Date());
            zuanShi1.setBeizhu("礼物收入");
            appMingxiJinbiDao.save(zuanShi1);

            if (gift.getGoumaifangshi() == Constants.goumaifangshi_jinbi) {
                //金币礼物 减少送礼人金币
                appUserDao.upJianjinbi(user.getId(), giftmoney);
                //增加送礼人消费金币明细
                AppMingxiJinbi zuanShi = new AppMingxiJinbi();
                zuanShi.setUserid(user.getId());
                zuanShi.setPic(user.getPic());
                zuanShi.setPhone(user.getLoginname());
                zuanShi.setNick(user.getNick());
                zuanShi.setLeixing(2);
                zuanShi.setNum(giftmoney);
                zuanShi.setCreatetime(new Date());
                zuanShi.setBeizhu("礼物赠送");
                appMingxiJinbiDao.save(zuanShi);
            } else if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                //钻石礼物  减少送礼人钻石
                appUserDao.upJianZuanShi(user.getId(), giftmoney);
                //增加送礼人消费钻石明细
                AppMingXiZuanShi zuanShi = new AppMingXiZuanShi();
                zuanShi.setUserid(user.getId());
                zuanShi.setPic(user.getPic());
                zuanShi.setPhone(user.getLoginname());
                zuanShi.setNick(user.getNick());
                zuanShi.setLeixing(2);
                zuanShi.setNum(giftmoney);
                zuanShi.setCreatetime(new Date());
                zuanShi.setBeizhu("礼物赠送");
                appMingXiZuanShiDao.saveOrUpdate(zuanShi);
            }
            //添加用户礼物墙
            AppUserGift appUserGift = appUserGiftDao.getGift(sluser.getId(), gift.getId());
            if (org.springframework.util.StringUtils.isEmpty(appUserGift)) {
                AppUserGift userGift = new AppUserGift();
                userGift.setUserid(sluser.getId());
                userGift.setCreatetime(new Date());
                userGift.setGiftid(gift.getId());
                userGift.setGiftname(gift.getGiftname());
                userGift.setGiftnum(giftNum.getNum());
                userGift.setPicture(gift.getPicture());
                userGift.setPhone(sluser.getLoginname());
                userGift.setPic(sluser.getPic());
                userGift.setNick(sluser.getNick());
                appUserGiftDao.saveOrUpdate(userGift);
            } else {
                appUserGiftDao.updateGiftNum(sluser.getId(), gift.getId(), giftNum.getNum());
            }
            //记录收礼明细
            AppMingXiShouLi shouLi = new AppMingXiShouLi();
            shouLi.setCreatetime(new Date());
            shouLi.setNick(sluser.getNick());
            shouLi.setPic(sluser.getPic());
            shouLi.setUserid(sluser.getId());
            shouLi.setPhone(sluser.getLoginname());
            shouLi.setGiftid(gift.getId());
            shouLi.setGiftname(gift.getGiftname());
            shouLi.setGiftpic(gift.getPicture());
            shouLi.setLiwunum(giftNum.getNum());
            shouLi.setFujianum(giftmoney);
            shouLi.setSluserid(user.getId());
            shouLi.setSlusernick(user.getNick());
            shouLi.setSluserphone(user.getLoginname());
            shouLi.setSluserpic(user.getPic());
            appMingXiShouLiDao.saveOrUpdate(shouLi);
            //记录送礼明细
            AppMingXiSongLi songli = new AppMingXiSongLi();
            songli.setCreatetime(new Date());
            songli.setNick(user.getNick());
            songli.setPic(user.getPic());
            songli.setUserid(user.getId());
            songli.setPhone(user.getLoginname());
            songli.setGiftid(gift.getId());
            songli.setGiftname(gift.getGiftname());
            songli.setGiftpic(gift.getPicture());
            songli.setLiwunum(giftNum.getNum());
            songli.setFujianum(giftmoney.longValue());
            songli.setSluserid(sluser.getId());
            songli.setSlusernick(sluser.getNick());
            songli.setSluserphone(sluser.getLoginname());
            songli.setSluserpic(sluser.getPic());
            appMingXiSongLiDao.saveOrUpdate(songli);

            addAward(sluser.getId(), getMoney);
            //添加送礼物任务
            appDailyTasController.getAwardOnce(user.getId(), ClientConstants.TaskOnce.GIFT_ONCE);
            //发送小秘书消息
            systemAlertService.giveGift(sluser.getTengxuncode(), user.getNick(), gift.getGiftname(), getCharm, getMoney);

            String key = String.format(RedisKeys.USER, sluser.getId());
            redisUtil.del(key);
            key = String.format(RedisKeys.USER, user.getId());
            redisUtil.del(key);

            AppSetup setup = appSetupService.get();
            /**************收、送礼物增加家族威望值开始***************/
            AppFamilyMember slFamilyMember = appFamilyMemberService.findByUserId(sluser.getId());
            if (slFamilyMember != null && slFamilyMember.getStatus() == 1) {
                if (setup.getShouliFamilyPrestige() > 0) {
                    appFamilyPrestigeService.addPrestisge(1, setup.getShouliFamilyPrestige(), sluser.getId(), "获赠礼物", slFamilyMember.getFid());
                }
            }
            AppFamilyMember songFamilyMember = appFamilyMemberService.findByUserId(user.getId());
            if (songFamilyMember != null && songFamilyMember.getStatus() == 1) {
                if (setup.getSongliFamilyPrestige() > 0) {
                    appFamilyPrestigeService.addPrestisge(2, setup.getSongliFamilyPrestige(), user.getId(), "赠送礼物", songFamilyMember.getFid());
                }
            }
            /**************收、送礼物增加家族威望值结束***************/

            /*************************增加亲密度开始*******************/
            AppIntimacyGuard appIntimacyGuard = new AppIntimacyGuard();
            BigDecimal intimacy = giftmoney.multiply(setup.getIntimacyZuanshi());
            appIntimacyGuard.setUserId(user.getId());
            appIntimacyGuard.setBeiUserId(sluser.getId());
            appIntimacyGuard.setIntimacy(intimacy);
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                appIntimacyGuard.setGiftMoney(giftmoney);
            } else {
                appIntimacyGuard.setGiftMoney(getMoney);
            }
            appIntimacyGuard.setCreatetime(new Date());
            addIntimacy(appIntimacyGuard);
            appIntimacyGuard.setUserId(sluser.getId());
            appIntimacyGuard.setBeiUserId(user.getId());
            appIntimacyGuard.setId(null);
            addIntimacy(appIntimacyGuard);
            /*************************增加亲密度结束*******************/


            return true;
        } catch (Exception e) {
            logger.error("赠送礼物异常：", e);
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean userGiftNum(AppUser sluser, AppUser user, BigDecimal giftmoney, AppGift gift, Integer giftNum) {
        try {
            BigDecimal getMoney = new BigDecimal("0");
            BigDecimal getCharm = new BigDecimal("0");
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                AppSetup appSetup = appSetupService.get();
                getMoney = giftmoney.multiply(appSetup.getJinDouExchangeForDiamonds()).setScale(2, BigDecimal.ROUND_HALF_UP);
                getCharm = giftmoney.multiply(new BigDecimal((appSetup.getDiamondsToCharmGrowth())).setScale(0, BigDecimal.ROUND_DOWN));
            }
            //增加收礼人金豆
            appUserDao.updatejinbi(sluser.getId(), getMoney);
            //增加收礼人魅力值
            appUserService.addCharm(sluser.getId(), (int) getCharm.longValue(), "获赠礼物");

            AppMingxiJinbi zuanShi1 = new AppMingxiJinbi();
            zuanShi1.setUserid(sluser.getId());
            zuanShi1.setPic(sluser.getPic());
            zuanShi1.setPhone(sluser.getLoginname());
            zuanShi1.setNick(sluser.getNick());
            zuanShi1.setLeixing(1);
            zuanShi1.setNum(getMoney);
            zuanShi1.setCreatetime(new Date());
            zuanShi1.setBeizhu("礼物收入");
            appMingxiJinbiDao.save(zuanShi1);

            if (gift.getGoumaifangshi() == Constants.goumaifangshi_jinbi) {
                //金币礼物 减少送礼人金币
                appUserDao.upJianjinbi(user.getId(), giftmoney);
                //增加送礼人消费金币明细
                AppMingxiJinbi zuanShi = new AppMingxiJinbi();
                zuanShi.setUserid(user.getId());
                zuanShi.setPic(user.getPic());
                zuanShi.setPhone(user.getLoginname());
                zuanShi.setNick(user.getNick());
                zuanShi.setLeixing(2);
                zuanShi.setNum(giftmoney);
                zuanShi.setCreatetime(new Date());
                zuanShi.setBeizhu("礼物赠送");
                appMingxiJinbiDao.save(zuanShi);
            } else if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                //钻石礼物  减少送礼人钻石
                appUserDao.upJianZuanShi(user.getId(), giftmoney);
                //增加送礼人消费钻石明细
                AppMingXiZuanShi zuanShi = new AppMingXiZuanShi();
                zuanShi.setUserid(user.getId());
                zuanShi.setPic(user.getPic());
                zuanShi.setPhone(user.getLoginname());
                zuanShi.setNick(user.getNick());
                zuanShi.setLeixing(2);
                zuanShi.setNum(giftmoney);
                zuanShi.setCreatetime(new Date());
                zuanShi.setBeizhu("礼物赠送");
                appMingXiZuanShiDao.saveOrUpdate(zuanShi);
            }
            //添加用户礼物墙
            AppUserGift appUserGift = appUserGiftDao.getGift(sluser.getId(), gift.getId());
            if (org.springframework.util.StringUtils.isEmpty(appUserGift)) {
                AppUserGift userGift = new AppUserGift();
                userGift.setUserid(sluser.getId());
                userGift.setCreatetime(new Date());
                userGift.setGiftid(gift.getId());
                userGift.setGiftname(gift.getGiftname());
                userGift.setGiftnum(giftNum);
                userGift.setPicture(gift.getPicture());
                userGift.setPhone(sluser.getLoginname());
                userGift.setPic(sluser.getPic());
                userGift.setNick(sluser.getNick());
                appUserGiftDao.saveOrUpdate(userGift);
            } else {
                appUserGiftDao.updateGiftNum(sluser.getId(), gift.getId(), giftNum);
            }
            //记录收礼明细
            AppMingXiShouLi shouLi = new AppMingXiShouLi();
            shouLi.setCreatetime(new Date());
            shouLi.setNick(sluser.getNick());
            shouLi.setPic(sluser.getPic());
            shouLi.setUserid(sluser.getId());
            shouLi.setPhone(sluser.getLoginname());
            shouLi.setGiftid(gift.getId());
            shouLi.setGiftname(gift.getGiftname());
            shouLi.setGiftpic(gift.getPicture());
            shouLi.setLiwunum(giftNum);
            shouLi.setFujianum(giftmoney);
            shouLi.setSluserid(user.getId());
            shouLi.setSlusernick(user.getNick());
            shouLi.setSluserphone(user.getLoginname());
            shouLi.setSluserpic(user.getPic());
            appMingXiShouLiDao.saveOrUpdate(shouLi);
            //记录送礼明细
            AppMingXiSongLi songli = new AppMingXiSongLi();
            songli.setCreatetime(new Date());
            songli.setNick(user.getNick());
            songli.setPic(user.getPic());
            songli.setUserid(user.getId());
            songli.setPhone(user.getLoginname());
            songli.setGiftid(gift.getId());
            songli.setGiftname(gift.getGiftname());
            songli.setGiftpic(gift.getPicture());
            songli.setLiwunum(giftNum);
            songli.setFujianum(giftmoney.longValue());
            songli.setSluserid(sluser.getId());
            songli.setSlusernick(sluser.getNick());
            songli.setSluserphone(sluser.getLoginname());
            songli.setSluserpic(sluser.getPic());
            appMingXiSongLiDao.saveOrUpdate(songli);

            //添加送礼物任务
            appDailyTasController.getAwardOnce(user.getId(), ClientConstants.TaskOnce.GIFT_ONCE);

            String key = String.format(RedisKeys.USER, sluser.getId());
            redisUtil.del(key);
            key = String.format(RedisKeys.USER, user.getId());
            redisUtil.del(key);

            AppSetup setup = appSetupService.get();
            /**************收、送礼物增加家族威望值开始***************/
            AppFamilyMember slFamilyMember = appFamilyMemberService.findByUserId(sluser.getId());
            if (slFamilyMember != null && slFamilyMember.getStatus() == 1) {
                if (setup.getShouliFamilyPrestige() > 0) {
                    appFamilyPrestigeService.addPrestisge(1, setup.getShouliFamilyPrestige(), sluser.getId(), "获赠礼物", slFamilyMember.getFid());
                }
            }
            AppFamilyMember songFamilyMember = appFamilyMemberService.findByUserId(user.getId());
            if (songFamilyMember != null && songFamilyMember.getStatus() == 1) {
                if (setup.getSongliFamilyPrestige() > 0) {
                    appFamilyPrestigeService.addPrestisge(2, setup.getSongliFamilyPrestige(), user.getId(), "赠送礼物", songFamilyMember.getFid());
                }
            }
            /**************收、送礼物增加家族威望值结束***************/

            /*************************增加亲密度开始*******************/
            AppIntimacyGuard appIntimacyGuard = new AppIntimacyGuard();
            BigDecimal intimacy = giftmoney.multiply(setup.getIntimacyZuanshi());
            appIntimacyGuard.setUserId(user.getId());
            appIntimacyGuard.setBeiUserId(sluser.getId());
            appIntimacyGuard.setIntimacy(intimacy);
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                appIntimacyGuard.setGiftMoney(giftmoney);
            } else {
                appIntimacyGuard.setGiftMoney(getMoney);
            }
            appIntimacyGuard.setCreatetime(new Date());
            addIntimacy(appIntimacyGuard);
            appIntimacyGuard.setUserId(sluser.getId());
            appIntimacyGuard.setBeiUserId(user.getId());
            appIntimacyGuard.setId(null);
            addIntimacy(appIntimacyGuard);
            /*************************增加亲密度结束*******************/
            return true;
        } catch (Exception e) {
            logger.error("赠送礼物异常：", e);
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    /**
     * 心动赠送礼物
     *
     * @param sluser
     * @param user
     * @param giftmoney
     * @param gift
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean beckoningUserGift(AppUser sluser, AppUser user, BigDecimal giftmoney, AppGift gift) {
        try {
            AppSetup setup = appSetupService.get();
            //将心动加入redis，过期后可重新心动
            redisUtil.set(RedisKeys.BECKONING + user.getId() + "-" + sluser.getId(), 1, setup.getRedisBeckoningTime() * 60);
            //心动绑定聊天主次关系
            MessageChatFrom chatFrom = messageChatFromService.get(user.getId(), sluser.getId());
            if (chatFrom == null) {
                messageChatFromService.add(user.getId(), sluser.getId());
            }
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    AppBeckoning beckoning = appBeckoningService.findBeckoningByUserId(sluser.getId(), user.getId());
                    AppBeckoning beckoning1 = appBeckoningService.findBeckoningByUserId(user.getId(), sluser.getId());
                    if (beckoning == null && beckoning1 == null) {
                        //双方首次心动，给对方发送打招呼语
                        TxIm.sendUserMsgNew(sluser.getTengxuncode(), user.getTengxuncode(), expressionsCommonService.randomOne(sluser.getSex()), "TIMTextElem");
                    }
                    //A对B心动 设置第一步
                    String keys = String.format(RedisKeys.BECKON_ALERT_ONE_TIP, user.getId(), sluser.getId());
                    redisUtil.set(keys, "1");
                    /**
                     *  设置心动礼物消息
                     *  礼物id+心动记录id
                     *  在对方回复的时候赠送礼物 增加账单记录
                     *  对方超时未回复删除redis key
                     */
                    String giftKey = String.format(RedisKeys.BECKON_GIFT, user.getId(), sluser.getId());
                    Set<String> giftKeys = redisUtil.keys(giftKey + "*");
                    if (giftKeys != null && giftKeys.size() > 0) {
                        int size = giftKeys.size();
                        redisUtil.set(giftKey + "_" + (size + 1), gift.getId(), Constants.RedisKeysLife.hours_12);
                    } else {
                        redisUtil.set(giftKey + "_" + 1, gift.getId(), Constants.RedisKeysLife.hours_12);
                    }

                    String str = "";
                    String str1 = "";
                    if(user.getSex() == 1) {
                        str = "小哥哥";
                        str1 = "他";
                    } else if(user.getSex() == 2) {
                        str = "小姐姐";
                        str1 = "她";
                    }
                    TxIm.sendUserMsgqbtAlert(sluser.getTengxuncode(), user.getTengxuncode(), "你的魅力吸引了对面的" + str + "，12小时内回复信息可获得"+ str1 +"的神秘礼物哦~");
                    //TxIm.sendUserGiftMsg(sluser.getTengxuncode(), user.getTengxuncode(), "送礼", gift.getPicture(), gift.getGiftname(), 2);
                }
            }).start();
            return true;
        } catch (Exception e) {
            logger.error("心动赠送礼物异常：", e);
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    /**
     * 女回复男心动后领取礼物
     * @param slUserId
     * @param userId
     * @param giftId
     * @return
     */
    @Override
    public boolean replyBeckoningGetGift(Integer slUserId, Integer userId, Integer giftId) {
        try {
            AppUser sluser = appUserDao.get(slUserId);
            AppUser user = appUserDao.get(userId);
            AppGift gift = appGiftDao.get(giftId);
            BigDecimal giftmoney = new BigDecimal(gift.getGoldmoney().toString());

            BigDecimal getMoney = new BigDecimal("0");
            BigDecimal getCharm = new BigDecimal("0");
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                AppSetup appSetup = appSetupService.get();
                getMoney = giftmoney.multiply(appSetup.getJinDouExchangeForDiamonds()).setScale(2, BigDecimal.ROUND_HALF_UP);
                getCharm = giftmoney.multiply(new BigDecimal((appSetup.getDiamondsToCharmGrowth())).setScale(0, BigDecimal.ROUND_DOWN));
            }

            //增加收礼人魅力值
            appUserDao.updatemeili(sluser.getId(), getCharm.longValue());
            //增加收礼人金币
            appUserDao.updatejinbi(sluser.getId(), getMoney);
            AppMingxiJinbi zuanShi1 = new AppMingxiJinbi();
            zuanShi1.setUserid(sluser.getId());
            zuanShi1.setPic(sluser.getPic());
            zuanShi1.setPhone(sluser.getLoginname());
            zuanShi1.setNick(sluser.getNick());
            zuanShi1.setLeixing(1);
            zuanShi1.setNum(getMoney);
            zuanShi1.setCreatetime(new Date());
            zuanShi1.setBeizhu("心动礼物收入");
            appMingxiJinbiDao.save(zuanShi1);

            //增加收礼人魅力值明细
            AppMingxiMeilizhi meilizhi = new AppMingxiMeilizhi();
            meilizhi.setUserid(sluser.getId());
            meilizhi.setPic(sluser.getPic());
            meilizhi.setPhone(sluser.getLoginname());
            meilizhi.setNick(sluser.getNick());
            meilizhi.setLeixing(1);
            meilizhi.setNum(getCharm.longValue());
            meilizhi.setCreatetime(new Date());
            meilizhi.setBeizhu("心动获赠礼物");
            appMingxiMeilizhiDao.save(meilizhi);
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_jinbi) {
                //金币礼物 减少送礼人金币
                appUserDao.upJianjinbi(user.getId(), giftmoney);
                //增加送礼人消费金币明细
                AppMingxiJinbi zuanShi = new AppMingxiJinbi();
                zuanShi.setUserid(user.getId());
                zuanShi.setPic(user.getPic());
                zuanShi.setPhone(user.getLoginname());
                zuanShi.setNick(user.getNick());
                zuanShi.setLeixing(2);
                zuanShi.setNum(giftmoney);
                zuanShi.setCreatetime(new Date());
                zuanShi.setBeizhu("心动礼物赠送");
                appMingxiJinbiDao.save(zuanShi);
            } else if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                //钻石礼物  减少送礼人钻石
                appUserDao.upJianZuanShi(user.getId(), giftmoney);
                //增加送礼人消费钻石明细
                AppMingXiZuanShi zuanShi = new AppMingXiZuanShi();
                zuanShi.setUserid(user.getId());
                zuanShi.setPic(user.getPic());
                zuanShi.setPhone(user.getLoginname());
                zuanShi.setNick(user.getNick());
                zuanShi.setLeixing(2);
                zuanShi.setNum(giftmoney);
                zuanShi.setCreatetime(new Date());
                zuanShi.setBeizhu("心动礼物赠送");
                appMingXiZuanShiDao.saveOrUpdate(zuanShi);
            }

            String key = String.format(RedisKeys.USER, sluser.getId());
            redisUtil.del(key);
            key = String.format(RedisKeys.USER, user.getId());
            redisUtil.del(key);
            //添加用户礼物墙
            AppUserGift appUserGift = appUserGiftDao.getGift(sluser.getId(), gift.getId());
            if (org.springframework.util.StringUtils.isEmpty(appUserGift)) {
                AppUserGift userGift = new AppUserGift();
                userGift.setUserid(sluser.getId());
                userGift.setCreatetime(new Date());
                userGift.setGiftid(gift.getId());
                userGift.setGiftname(gift.getGiftname());
                userGift.setGiftnum(1);
                userGift.setPicture(gift.getPicture());
                userGift.setPhone(sluser.getLoginname());
                userGift.setPic(sluser.getPic());
                userGift.setNick(sluser.getNick());
                appUserGiftDao.saveOrUpdate(userGift);
            } else {
                appUserGiftDao.updateGiftNum(sluser.getId(), gift.getId(), 1);
            }
            //记录收礼明细
            AppMingXiShouLi shouLi = new AppMingXiShouLi();
            shouLi.setCreatetime(new Date());
            shouLi.setNick(sluser.getNick());
            shouLi.setPic(sluser.getPic());
            shouLi.setUserid(sluser.getId());
            shouLi.setPhone(sluser.getLoginname());
            shouLi.setGiftid(gift.getId());
            shouLi.setGiftname(gift.getGiftname());
            shouLi.setGiftpic(gift.getPicture());
            shouLi.setLiwunum(1);
            shouLi.setFujianum(giftmoney);
            shouLi.setSluserid(user.getId());
            shouLi.setSlusernick(user.getNick());
            shouLi.setSluserphone(user.getLoginname());
            shouLi.setSluserpic(user.getPic());
            appMingXiShouLiDao.saveOrUpdate(shouLi);
            //记录送礼明细
            AppMingXiSongLi songli = new AppMingXiSongLi();
            songli.setCreatetime(new Date());
            songli.setNick(user.getNick());
            songli.setPic(user.getPic());
            songli.setUserid(user.getId());
            songli.setPhone(user.getLoginname());
            songli.setGiftid(gift.getId());
            songli.setGiftname(gift.getGiftname());
            songli.setGiftpic(gift.getPicture());
            songli.setLiwunum(1);
            songli.setFujianum(giftmoney.longValue());
            songli.setSluserid(sluser.getId());
            songli.setSlusernick(sluser.getNick());
            songli.setSluserphone(sluser.getLoginname());
            songli.setSluserpic(sluser.getPic());
            appMingXiSongLiDao.saveOrUpdate(songli);
            AppSetup setup = appSetupService.get();
            /*******************添加心动记录开始***********************/
            AppBeckoning beckoningByUserId = appBeckoningService.findBeckoningByUserId(sluser.getId(), user.getId());
            AppBeckoning appBeckoning = new AppBeckoning();
            appBeckoning.setUserId(user.getId());
            appBeckoning.setBeiUserId(sluser.getId());
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                appBeckoning.setGiftMoney(giftmoney);
            } else {
                appBeckoning.setGiftMoney(getMoney);
            }
            appBeckoning.setGiftNum(1);
            if (beckoningByUserId == null) {
                appBeckoning.setIsMutual(2);
            } else {
                appBeckoning.setIsMutual(1);
                //更新双方心动状态为相互心动
                appBeckoningService.updateBeckoningMutual(sluser.getId(), user.getId());
            }
            appBeckoning.setCreatetime(new Date());
            appBeckoning.setGiftFangShi(gift.getGoumaifangshi());
            appBeckoning.setIntimacy(gift.getIntimacy());
            appBeckoning.setGiftId(gift.getId());
            appBeckoningService.addBeckoning(appBeckoning);

            /*************************增加亲密度开始*******************/
            AppIntimacyGuard appIntimacyGuard = new AppIntimacyGuard();
            BigDecimal intimacy = giftmoney.multiply(setup.getIntimacyZuanshi());
            appIntimacyGuard.setUserId(user.getId());
            appIntimacyGuard.setBeiUserId(sluser.getId());
            appIntimacyGuard.setIntimacy(intimacy);
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                appIntimacyGuard.setGiftMoney(giftmoney);
            } else {
                appIntimacyGuard.setGiftMoney(getMoney);
            }
            appIntimacyGuard.setCreatetime(new Date());
            addIntimacy(appIntimacyGuard);
            appIntimacyGuard.setUserId(sluser.getId());
            appIntimacyGuard.setBeiUserId(user.getId());
            appIntimacyGuard.setId(null);
            addIntimacy(appIntimacyGuard);
            return true;
        } catch (Exception e) {
            logger.error("心动赠送礼物异常：", e);
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    //夫妻相匹配赠送礼物
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean spouseMatchUserGift(AppUser sluser, AppUser user, BigDecimal giftmoney, AppGift gift) {
        try {
            BigDecimal getMoney = new BigDecimal("0");
            BigDecimal getCharm = new BigDecimal("0");
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                AppSetup appSetup = appSetupService.get();
                getMoney = giftmoney.multiply(appSetup.getJinDouExchangeForDiamonds()).setScale(2, BigDecimal.ROUND_HALF_UP);
                getCharm = giftmoney.multiply(new BigDecimal((appSetup.getDiamondsToCharmGrowth())).setScale(0, BigDecimal.ROUND_DOWN));
            }

            //增加收礼人魅力值
            appUserDao.updatemeili(sluser.getId(), getCharm.longValue());
            //增加收礼人金币
            appUserDao.updatejinbi(sluser.getId(), getMoney);
            AppMingxiJinbi zuanShi1 = new AppMingxiJinbi();
            zuanShi1.setUserid(sluser.getId());
            zuanShi1.setPic(sluser.getPic());
            zuanShi1.setPhone(sluser.getLoginname());
            zuanShi1.setNick(sluser.getNick());
            zuanShi1.setLeixing(1);
            zuanShi1.setNum(getCharm);
            zuanShi1.setCreatetime(new Date());
            zuanShi1.setBeizhu("心动礼物收入");
            appMingxiJinbiDao.save(zuanShi1);

            //增加收礼人魅力值明细
            AppMingxiMeilizhi meilizhi = new AppMingxiMeilizhi();
            meilizhi.setUserid(sluser.getId());
            meilizhi.setPic(sluser.getPic());
            meilizhi.setPhone(sluser.getLoginname());
            meilizhi.setNick(sluser.getNick());
            meilizhi.setLeixing(1);
            meilizhi.setNum(getCharm.longValue());
            meilizhi.setCreatetime(new Date());
            meilizhi.setBeizhu("心动获赠礼物");
            appMingxiMeilizhiDao.save(meilizhi);
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_jinbi) {
                //金币礼物 减少送礼人金币
                appUserDao.upJianjinbi(user.getId(), giftmoney);
                //增加送礼人消费金币明细
                AppMingxiJinbi zuanShi = new AppMingxiJinbi();
                zuanShi.setUserid(user.getId());
                zuanShi.setPic(user.getPic());
                zuanShi.setPhone(user.getLoginname());
                zuanShi.setNick(user.getNick());
                zuanShi.setLeixing(2);
                zuanShi.setNum(giftmoney);
                zuanShi.setCreatetime(new Date());
                zuanShi.setBeizhu("心动礼物赠送");
                appMingxiJinbiDao.save(zuanShi);
            } else if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                //钻石礼物  减少送礼人钻石
                appUserDao.upJianZuanShi(user.getId(), giftmoney);
                //增加送礼人消费钻石明细
                AppMingXiZuanShi zuanShi = new AppMingXiZuanShi();
                zuanShi.setUserid(user.getId());
                zuanShi.setPic(user.getPic());
                zuanShi.setPhone(user.getLoginname());
                zuanShi.setNick(user.getNick());
                zuanShi.setLeixing(2);
                zuanShi.setNum(giftmoney);
                zuanShi.setCreatetime(new Date());
                zuanShi.setBeizhu("心动礼物赠送");
                appMingXiZuanShiDao.saveOrUpdate(zuanShi);
            }

            String key = String.format(RedisKeys.USER, sluser.getId());
            redisUtil.del(key);
            key = String.format(RedisKeys.USER, user.getId());
            redisUtil.del(key);

            //添加用户礼物墙
            AppUserGift appUserGift = appUserGiftDao.getGift(sluser.getId(), gift.getId());
            if (org.springframework.util.StringUtils.isEmpty(appUserGift)) {
                AppUserGift userGift = new AppUserGift();
                userGift.setUserid(sluser.getId());
                userGift.setCreatetime(new Date());
                userGift.setGiftid(gift.getId());
                userGift.setGiftname(gift.getGiftname());
                userGift.setGiftnum(1);
                userGift.setPicture(gift.getPicture());
                userGift.setPhone(sluser.getLoginname());
                userGift.setPic(sluser.getPic());
                userGift.setNick(sluser.getNick());
                appUserGiftDao.saveOrUpdate(userGift);
            } else {
                appUserGiftDao.updateGiftNum(sluser.getId(), gift.getId(), 1);
            }
            //记录收礼明细
            AppMingXiShouLi shouLi = new AppMingXiShouLi();
            shouLi.setCreatetime(new Date());
            shouLi.setNick(sluser.getNick());
            shouLi.setPic(sluser.getPic());
            shouLi.setUserid(sluser.getId());
            shouLi.setPhone(sluser.getLoginname());
            shouLi.setGiftid(gift.getId());
            shouLi.setGiftname(gift.getGiftname());
            shouLi.setGiftpic(gift.getPicture());
            shouLi.setLiwunum(1);
            shouLi.setFujianum(giftmoney);
            shouLi.setSluserid(user.getId());
            shouLi.setSlusernick(user.getNick());
            shouLi.setSluserphone(user.getLoginname());
            shouLi.setSluserpic(user.getPic());
            appMingXiShouLiDao.saveOrUpdate(shouLi);
            //记录送礼明细
            AppMingXiSongLi songli = new AppMingXiSongLi();
            songli.setCreatetime(new Date());
            songli.setNick(user.getNick());
            songli.setPic(user.getPic());
            songli.setUserid(user.getId());
            songli.setPhone(user.getLoginname());
            songli.setGiftid(gift.getId());
            songli.setGiftname(gift.getGiftname());
            songli.setGiftpic(gift.getPicture());
            songli.setLiwunum(1);
            songli.setFujianum(giftmoney.longValue());
            songli.setSluserid(sluser.getId());
            songli.setSlusernick(sluser.getNick());
            songli.setSluserphone(sluser.getLoginname());
            songli.setSluserpic(sluser.getPic());
            appMingXiSongLiDao.saveOrUpdate(songli);
            AppSetup setup = appSetupService.get();
            /*******************添加心动记录开始***********************/
            AppBeckoning beckoningByUserId = appBeckoningService.findBeckoningByUserId(sluser.getId(), user.getId());
            AppBeckoning appBeckoning = new AppBeckoning();
            appBeckoning.setUserId(user.getId());
            appBeckoning.setBeiUserId(sluser.getId());
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                appBeckoning.setGiftMoney(giftmoney);
            } else {
                appBeckoning.setGiftMoney(getMoney);
            }
            appBeckoning.setGiftNum(1);
            if (beckoningByUserId == null) {
                appBeckoning.setIsMutual(2);
            } else {
                appBeckoning.setIsMutual(1);
                //更新双方心动状态为相互心动
                appBeckoningService.updateBeckoningMutual(sluser.getId(), user.getId());
            }
            appBeckoning.setCreatetime(new Date());
            appBeckoning.setGiftFangShi(gift.getGoumaifangshi());
            appBeckoning.setIntimacy(gift.getIntimacy());
            appBeckoning.setGiftId(gift.getId());
            appBeckoningService.addBeckoning(appBeckoning);

            //将心动加入redis，过期后可重新心动
            redisUtil.set(RedisKeys.BECKONING + user.getId() + "-" + sluser.getId(), 1, setup.getRedisBeckoningTime() * 60);
            /*******************添加心动记录结束***********************/


            /*************************增加亲密度开始*******************/
            AppIntimacyGuard appIntimacyGuard = new AppIntimacyGuard();
            BigDecimal intimacy = giftmoney.multiply(setup.getIntimacyZuanshi());
            appIntimacyGuard.setUserId(user.getId());
            appIntimacyGuard.setBeiUserId(sluser.getId());
            appIntimacyGuard.setIntimacy(intimacy);
            if (gift.getGoumaifangshi() == Constants.goumaifangshi_zuanshi) {
                appIntimacyGuard.setGiftMoney(giftmoney);
            } else {
                appIntimacyGuard.setGiftMoney(getMoney);
            }
            appIntimacyGuard.setCreatetime(new Date());
            addIntimacy(appIntimacyGuard);
            appIntimacyGuard.setUserId(sluser.getId());
            appIntimacyGuard.setBeiUserId(user.getId());
            appIntimacyGuard.setId(null);
            addIntimacy(appIntimacyGuard);
            /*************************增加亲密度结束*******************/
            return true;
        } catch (Exception e) {
            logger.error("心动赠送礼物异常：", e);
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = java.lang.Exception.class)
    public boolean userBeibaoGift(AppUser sluser, AppUser user, AppKnapsack knapsack, AppGift gift, AppGiftNum giftNum) {
        try {
            int giftmoney = (gift.getGoldmoney() * giftNum.getNum());
            //增加收礼人魅力值
            appUserDao.updatemeili(sluser.getId(), giftmoney);
            //增加收礼人魅力值明细
            AppMingxiMeilizhi meilizhi = new AppMingxiMeilizhi();
            meilizhi.setUserid(sluser.getId());
            meilizhi.setPic(sluser.getPic());
            meilizhi.setPhone(sluser.getLoginname());
            meilizhi.setNick(sluser.getNick());
            meilizhi.setLeixing(1);
            meilizhi.setNum(giftmoney);
            meilizhi.setCreatetime(new Date());
            meilizhi.setBeizhu("礼物赠送");
            appMingxiMeilizhiDao.save(meilizhi);
            //减少背包礼物数量
            appKnapsackDao.jianshao(knapsack.getId(), giftNum.getNum());
            //添加用户礼物墙
            AppUserGift appUserGift = appUserGiftDao.getGift(sluser.getId(), gift.getId());
            if (org.springframework.util.StringUtils.isEmpty(appUserGift)) {
                AppUserGift userGift = new AppUserGift();
                userGift.setUserid(sluser.getId());
                userGift.setCreatetime(new Date());
                userGift.setGiftid(gift.getId());
                userGift.setGiftname(gift.getGiftname());
                userGift.setGiftnum(giftNum.getNum());
                userGift.setPicture(gift.getPicture());
                userGift.setPhone(sluser.getLoginname());
                userGift.setPic(sluser.getPic());
                userGift.setNick(sluser.getNick());
                appUserGiftDao.saveOrUpdate(userGift);
            } else {
                appUserGiftDao.updateGiftNum(sluser.getId(), gift.getId(), giftNum.getNum());
            }
            //记录收礼明细
            AppMingXiShouLi shouLi = new AppMingXiShouLi();
            shouLi.setCreatetime(new Date());
            shouLi.setNick(sluser.getNick());
            shouLi.setPic(sluser.getPic());
            shouLi.setUserid(sluser.getId());
            shouLi.setPhone(sluser.getLoginname());
            shouLi.setGiftid(gift.getId());
            shouLi.setGiftname(gift.getGiftname());
            shouLi.setGiftpic(gift.getPicture());
            shouLi.setLiwunum(giftNum.getNum());
            shouLi.setFujianum(new BigDecimal(giftmoney));
            shouLi.setSluserid(user.getId());
            shouLi.setSlusernick(user.getNick());
            shouLi.setSluserphone(user.getLoginname());
            shouLi.setSluserpic(user.getPic());
            appMingXiShouLiDao.saveOrUpdate(shouLi);
            //记录送礼明细
            AppMingXiSongLi songli = new AppMingXiSongLi();
            songli.setCreatetime(new Date());
            songli.setNick(user.getNick());
            songli.setPic(user.getPic());
            songli.setUserid(user.getId());
            songli.setPhone(user.getLoginname());
            songli.setGiftid(gift.getId());
            songli.setGiftname(gift.getGiftname());
            songli.setGiftpic(gift.getPicture());
            songli.setLiwunum(giftNum.getNum());
            songli.setFujianum(giftmoney);
            songli.setSluserid(sluser.getId());
            songli.setSlusernick(sluser.getNick());
            songli.setSluserphone(sluser.getLoginname());
            songli.setSluserpic(sluser.getPic());
            appMingXiSongLiDao.saveOrUpdate(songli);
            return true;
        } catch (Exception e) {
            //导入失败  事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public AppGift getRandomGift() {
        return appGiftDao.getRandomGift();
    }

    @Override
    public AppGift randomGift(BigDecimal diamonds) {
        return appGiftDao.randomGift(diamonds);
    }

    @Override
    public AppGift getLowMoneyGift() {
        return appGiftDao.getLowMoneyGift();
    }


    /**
     * 给上级提成收益
     *
     */
    private void addAward(Integer userId, BigDecimal goldCoin) {
        AppInvitationRecord record = appInvitationRecordService.getShangji(userId);
        if (record != null) {
            AppSetup appSetup = appSetupService.get();
            BigDecimal divide = goldCoin.multiply(appSetup.getGiftInvite()).setScale(2, BigDecimal.ROUND_HALF_UP);
            appUserService.updatejinbi(record.getPuserid(), divide);
            appMingxiJinbiService.addMingxi(record.getPuserid(), divide, 1, "邀请用户礼物收益提成");
        }
    }
}
